package com.comtrade.map;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.Log;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;

import com.example.beaconapk.R;

public class BeaconView extends View {
	private GestureDetector simpledetector;
	private float x,y;
	private Drawable image;
	private String message;
	private View parentView;
	
	public BeaconView(Context context) {
		super(context);
		// TODO Auto-generated constructor stub
	}
	public BeaconView(Context context, float x, float y, String message,TouchView parentView){
		super(context);
		simpledetector=new GestureDetector(getContext(), simpleOnGestureListener);
		this.x=x;
		this.y=y;
		this.message=message;
		this.parentView=parentView;
		if (parentView!=null) Log.d("Parrent","Radi");
		if (x*100>=50) x-=50;
		if (y*100>=50) y-=50;
		Log.d("sirine",x+" "+parentView.getmMap().getBounds().width());
		Bitmap slika = BitmapFactory.decodeResource(getResources(), R.drawable.beacon_gray);
		image = new BitmapDrawable(getResources(), slika);	
		int h,w; h=50; w=50;
		image.setBounds(0,0,h,w);
	//	this.image =new BitmapDrawable(getResources(),Bitmap.createScaledBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.beacon_gray), 50, 50, false));
	}
	@Override
    public boolean onTouchEvent(MotionEvent ev) {
        // Let the ScaleGestureDetector inspect all events.
		simpledetector.onTouchEvent(ev);
        return true;
    }
	@Override
	public void onDraw(Canvas canvas){
		super.onDraw(canvas);		
		canvas.save();
		canvas.translate(x*100, y*100);
		image.draw(canvas);
		canvas.restore();
		parentView.invalidate();
	}

	public float getX() {
		return x;
	}
	public void setX(float x) {
		this.x = x;
	}
	public float getY() {
		return y;
	}
	public void setY(float y) {
		this.y = y;
	}
	public Drawable getImage() {
		return image;
	}
	public void setImage(Drawable image) {
		this.image = image;
	}
	public String getMessage() {
		return message;
	}
	public void setMessage(String message) {
		this.message = message;
	}
    SimpleOnGestureListener simpleOnGestureListener=new SimpleOnGestureListener(){
      public boolean onSingleTapUp(MotionEvent e) {
    	  Log.d("BEACON","TOUCH EVENT");
    	  Toast t = Toast.makeText(getContext(), message, Toast.LENGTH_SHORT);
  		  t.show();
    	  return true;
      };
    };

}
